import { getToken, setToken, removeToken } from "@/utils/auth";
import { loginApi, getInfoApi } from "@/api/user";
// import { resetRouter } from "@/router";
const state = {
  token: getToken(),
  userInfo: {},
};
const mutations = {
  setToken(state, data) {
    state.token = data;
    setToken(data);
  },
  removeToken(state) {
    state.token = null;
    removeToken();
  },
  setUserInfo(state, data) {
    state.userInfo = data;
  },
  removeuserInfo(state) {
    state.userInfo = {};
  },
};
const actions = {
  async loginAction(context, data) {
    let token = await loginApi(data);
    console.log(token);
    context.commit("setToken", token);
  },
  async getUserProfileAction(context) {
    let res = await getInfoApi();
    // console.log(res);
    let res2 = await getUserInfoApi(res.userId);
    context.commit("setUserInfo", { ...res, ...res2 });
    return res;
  },

  quitAction(context) {
    context.commit("removeToken");
    context.commit("removeuserInfo");
    // context.commit("permission/setRoutes", [], { root: true });
    // resetRouter();
  },
};
export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
